<?php

class Silk_Pms_Block_Activity_Abstract extends Mage_Core_Block_Template
{
    private $_activity = null;
    private $_today;

    /**
     * @return Mage_Core_Model_Abstract|null 获取数据表传入id后对应该条记录
     * @throws Exception
     */
    public function getActivity()
    {
        if($this->_activity) return $this->_activity;

        $this->_activity = Mage::getModel('pms/activity')->load($this->getRequest()->getParam('id'));

        if(!$this->_activity || !$this->_activity->getId())
        {
            throw new Exception("活动不存在！");
        }
        return $this->_activity;
    }

    /**
     * @param array $skus 商品sku组成的数组
     * @return mixed 包含商品所有信息的集合
     * @throws Exception
     */
    public function getProductsBySkus($skus = array())
    {
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('*')->addAttributeToFilter("sku",$skus);
            $collection->addWebsiteFilter(array($this->getActivity()->getWebsiteId()));
        $collection->addAttributeToFilter('visibility', array(
            'neq' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE))
            ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));
        $isShowOutOfStock = Mage::getStoreConfigFlag('cataloginventory/options/show_out_of_stock');
        if(!$isShowOutOfStock)
        {
            Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
        }

        return $collection;
    }

    /**
     * @param $path 图片地址的url前缀
     * @return string
     */
    public function getActivityImageUrl($path)
    {
        return Mage::getBaseUrl('meida').$path;
    }

    /**
     * @param $price 参数商品输出价格
     * @return float
     */
    public function formatPrice($price)
    {
        return Mage::app()->getStore()->convertPrice($price);
    }

    /**
     * 返回网站的url
     * @return string
     */
    public function baseImageurl()
    {
        return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    }

    /**
     * @param $date 参数是活动模板设定的截止日期
     * @return array
     */
    public function countdown($date)
    {
        $temp = explode(" ",$date);
        $arr = array_splice($temp, 1, 2);
        $end[] = explode("-",implode("",$temp));
        $end[] = explode(":",implode("",$arr));
        return $end;

    }

    /**
     * @return bool 判断日期是否过期和状态是否禁用
     * @throws Exception
     */
    public function isshow()
    {
        date_default_timezone_set('PRC');
        $endat = $this->getActivity()->getEndAt();
        $endday = strtotime($endat);
        $this->_today = strtotime(date('Y-m-d H:i:s'));
        if($this->_today > $endday){
            return false;
        }else{
            $status = $this->getActivity()->getStatus();
            if($status){
                return true;
            }else{
                return false;
            }
        }
    }

    /**判断是否有库存
     * @param $_product
     * @return mixed
     */
    public function getstock($_product)
    {
        $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
        return $qty;
    }

    /**
     * 判断商品是否下架
     * @param $productId
     * @param $websiteId
     * @return mixed
     */
    public function hasStock($productId,$websiteId){
        $collection = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*');
        $collection->addAttributeToFilter('entity_id',$productId);
        if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
            $collection->joinField('is_in_stock',
                'cataloginventory/stock_item',
                'is_in_stock',
                'product_id=entity_id',
                '{{table}}.stock_id=1 AND {{table}}.website_id = ' . $websiteId,
                'left');
        }
        $product = $collection->getFirstItem();
        return $product->getIsInStock();
    }


}